import { sandlist } from '../api/rendal.ts';
const IndexModel = {
  namespace: 'sand',

  state: {
    sanlist: [],
    adddata: [],
  },

  effects: {
    *Get_sand({ payload }, { call, put }) {
      const data = yield sandlist({ payload });
      // console.log(data)
      yield put({ type: 'SET_SAND', payload: data.data.RECORDS });
    },
  },
  reducers: {
    SET_SAND(state, { payload }) {
      // console.log(state)
      state.sanlist = payload;
      return {
        ...state,
      };
    },
    // 编辑
    EDITS(state,{payload}){
      // console.log(payload)
      const {editindex,title,floor_num} =payload
      const newedit = JSON.parse(JSON.stringify(state))
      newedit.sanlist[editindex].title = title
      newedit.sanlist[editindex].floor_num =floor_num
     return{
      ...newedit

     }
    },
    DELSHA(state, { payload }) {
      const newState = JSON.parse(JSON.stringify(state));
      newState.sanlist.splice(payload, 1);
      return {
        ...newState,
      };
    },
    // 添加和移除沙盘
    CHANGE(state, { payload }) {
      const newtitle = JSON.parse(JSON.stringify(state.adddata));
      // console.log(newtitle)
      const index = newtitle.findIndex((item) => item === payload);
      // console.log(index)
      if (index === -1) {
        newtitle.push(payload);
      } else {
        newtitle.splice(index, 1);
      }
      return {
        ...state,
        adddata: newtitle,
      };
    },
     // 添加数据
  ADDSHA(state, { payload }) {
    // console.log('6666666666');
    // console.log(state, 'state', payload, '1111111');
    const { floor_num, title } = payload;
    const newState = JSON.parse(JSON.stringify(state));
    newState.sanlist.push({
      id: newState.sanlist[newState.sanlist.length - 1].id + 1,
      title: title,
      unit: null,
      elevator: null,
      floor_num: floor_num,
      room_num: null,
      sale_status: '30',
      status: 1,
    });
    return {
      ...newState,
    };
  },
  },
 
};

export default IndexModel;
